<?php

namespace backend\components;

use yii\web\ErrorHandler;
use yii\web\Response;

/**
 * 后台组建异常捕获
 */
class AdminErrorHandler extends ErrorHandler
{

    /**
     * 重写渲染异常页面方法
     * @param \Exception $exception
     */
    public function renderException($exception)
    {
        $error = [
            'code' => 500,
            // 'msg' => $exception->getMessage(),
            'msg' => '系统错误',
        ];

        if (YII_DEBUG) {
            $error['data'] = [
                'msg' => $exception->getMessage(),
                'code' => $exception->getCode(),
                'line' => $exception->getLine(),
                'file' => $exception->getFile(),
                'trace' => $exception->getTraceAsString(),
            ];
        }
        $response = \Yii::$app->response;
        $response->format = Response::FORMAT_JSON;
        $response->data = $error;
        $response->send();
        exit(0);
    }

}